package com.xjt.common;

import javax.servlet.http.HttpServletRequest;

/**
 * 获取用户IP的工具类
 * 
 * @author yajun
 */
public class IpUtils {

    /**
     * 获取IP地址
     * 
     * @param request 用户请求
     * @return IP
     */
    public static String getIpAddr(HttpServletRequest request) {
        String ip = request.getHeader("x-forwarded-for");
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }
        if (ip != null && ip.indexOf(",") != -1) {
            return ip.split(",")[0];
        }
        return ip;
    }

}
